package com.ruoyi.resource.service.impl;

import java.util.List;

import com.ruoyi.bussiness.domain.SysBusinessDynamic;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.OssUtil;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.resource.mapper.SysBusinessResourceVideoMapper;
import com.ruoyi.resource.domain.SysBusinessResourceVideo;
import com.ruoyi.resource.service.ISysBusinessResourceVideoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 视频资源Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-22
 */
@Service
public class SysBusinessResourceVideoServiceImpl implements ISysBusinessResourceVideoService
{
    @Autowired
    private SysBusinessResourceVideoMapper sysBusinessResourceVideoMapper;

    /**
     * 查询视频资源
     *
     * @param videoId 视频资源主键
     * @return 视频资源
     */
    @Override
    public SysBusinessResourceVideo selectSysBusinessResourceVideoByVideoId(Long videoId)
    {
        return sysBusinessResourceVideoMapper.selectSysBusinessResourceVideoByVideoId(videoId);
    }

    /**
     * 查询视频资源列表
     *
     * @param sysBusinessResourceVideo 视频资源
     * @return 视频资源
     */
    @Override
    public List<SysBusinessResourceVideo> selectSysBusinessResourceVideoList(SysBusinessResourceVideo sysBusinessResourceVideo)
    {
        return sysBusinessResourceVideoMapper.selectSysBusinessResourceVideoList(sysBusinessResourceVideo);
    }

    /**
     * 新增视频资源
     *
     * @param sysBusinessResourceVideo 视频资源
     * @return 结果
     */
    @Override
    public int insertSysBusinessResourceVideo(SysBusinessResourceVideo sysBusinessResourceVideo)
    {
        sysBusinessResourceVideo.setCreateTime(DateUtils.getNowDate());
        return sysBusinessResourceVideoMapper.insertSysBusinessResourceVideo(sysBusinessResourceVideo);
    }

    /**
     * 修改视频资源
     *
     * @param sysBusinessResourceVideo 视频资源
     * @return 结果
     */
    @Override
    public int updateSysBusinessResourceVideo(SysBusinessResourceVideo sysBusinessResourceVideo)
    {
        sysBusinessResourceVideo.setUpdateTime(DateUtils.getNowDate());
        return sysBusinessResourceVideoMapper.updateSysBusinessResourceVideo(sysBusinessResourceVideo);
    }

    /**
     * 批量删除视频资源
     *
     * @param videoIds 需要删除的视频资源主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysBusinessResourceVideoByVideoIds(Long[] videoIds)
    {
        for(Long i : videoIds){
            SysBusinessResourceVideo temp =  sysBusinessResourceVideoMapper.selectSysBusinessResourceVideoByVideoId(i);
            if(temp != null ){
                if(StringUtils.isNotEmpty(temp.getVideoUrl())){
                    String fileName = temp.getVideoUrl().replace("https://bridgebj.oss-cn-beijing.aliyuncs.com/","");
                    System.out.println(fileName);
                    try {
                        OssUtil.deleteOssFile(fileName);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                if(StringUtils.isNotEmpty(temp.getVideoCover())){
                    String fileName = temp.getVideoCover().replace("https://bridgebj.oss-cn-beijing.aliyuncs.com/","");
                    System.out.println(fileName);
                    try {
                        OssUtil.deleteOssFile(fileName);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
            int row = sysBusinessResourceVideoMapper.deleteSysBusinessResourceVideoByVideoId(i);
        }
        return 1;
    }

    /**
     * 删除视频资源信息
     *
     * @param videoId 视频资源主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysBusinessResourceVideoByVideoId(Long videoId)
    {
        SysBusinessResourceVideo temp =  sysBusinessResourceVideoMapper.selectSysBusinessResourceVideoByVideoId(videoId);
        if(temp != null ){
            if(StringUtils.isNotEmpty(temp.getVideoUrl())){
                String fileName = temp.getVideoUrl().replace("https://bridgebj.oss-cn-beijing.aliyuncs.com/","");
                System.out.println(fileName);
                try {
                    OssUtil.deleteOssFile(fileName);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            if(StringUtils.isNotEmpty(temp.getVideoCover())){
                String fileName = temp.getVideoCover().replace("https://bridgebj.oss-cn-beijing.aliyuncs.com/","");
                System.out.println(fileName);
                try {
                    OssUtil.deleteOssFile(fileName);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        int row = sysBusinessResourceVideoMapper.deleteSysBusinessResourceVideoByVideoId(videoId);

        return 1;
    }

    @Override
    public int backVideoIds(Long[] videoIds) {

        for(Long i : videoIds){
//            System.out.println(i);
            SysBusinessResourceVideo sysBusinessResourceVideo = new SysBusinessResourceVideo();
            sysBusinessResourceVideo.setVideoId(i);
            sysBusinessResourceVideo.setStatus("2");
            sysBusinessResourceVideoMapper.BackSysBusinessDynamic(sysBusinessResourceVideo);
        }
        return 1;
    }
}
